<?php
namespace app\admin\model;
use think\Db;
class Member extends BaseModel
{
    protected $table = 'cz_users';
    protected $rule = [
        'uid'  =>  '',
    ];
    protected $msg = [
        'uid'  =>  '',
    ];
    protected $return = array();
    //获取用户列表
    public function getMemberList(){
        $where = [];
        if (input("post.")) {
            $request = input("post.");
            if (! empty($request['username'])) {
                $where["user_name"] = [
                    "like",
                    "%" . $request['username'] . "%"
                ];
            }
        }
        $list = $this->alias('u')
        ->join('agent a','a.agent_id = u.agent','left')
        ->join('bank b','b.bankId = u.bankName','left')
        ->where($where)->order("u.user_id desc")->field('u.*,a.name,a.level,b.bankName bName')->paginate("10");
        return $list;
    }

    // 获取后台用户管理
    public function getAdminsLists(){
        $where = [];
        if(input('post.')){
            $request = input('post.');
            if(!empty($request['username'])){
                $where['username'] = [
                    "like",
                    "%" . $request['username'] . "%"
                ];
            }
            $list = Db::name('admin')
            ->where($where)->order("id desc")->paginate("10");
        return $list;
        }
    }
    //获取用户信息
    public function getMember($id){
        $where = array();
        $where ['u.user_id'] = $id;
        $userInfo = $this->alias('u')
                    ->where($where)->find();
        return $userInfo;
    }
    //修改会员状态
    public function memberCheck($id){
        $userInfo = Db::name('users')->where("user_id",$id)->find();
        if($userInfo['status'] == 0){
            $res = Db::name('users')->where("user_id",$id)->update(['status'=>1]);
        }else{
            $res = Db::name('users')->where("user_id",$id)->update(['status'=>0,'create_time'=>time()]);
        }
        if($res){
            $this->return ['status'] = 1;
            $this->return ['msg'] = "修改成功";
        }else{
            $this->return ['status'] = 0;
            $this->return ['msg'] = "修改失败";
        }
        return $this->return;
    }
    //删除会员
    public function memberDel($id){
        $res = $this->where("user_id",$id)->delete();
        if($res){
            $this->return ['status'] = 1;
            $this->return ['msg'] = "删除成功";
        }else{
            $this->return ['status'] = 0;
            $this->return ['msg'] = "删除失败";
        }
        return $this->return;
    }
    //修改会员信息
    public function memberEdit($data){
        
        $id = $data['user_id'];
        unset($data['user_id']);
        unset($data['p_user']);
        $where['user_id'] = $id;
		// $update['username'] = $data['']
        $res = $this->where($where)->update($data);
        if($res){
            return 1;
        }else{
            return 2;
        }
    }
    // 修改充值筹码数量
    public function rechargeEdit($data){
        // echo "<pre>";
        // print_r($data);die;
        $where['user_id'] = $data['user_id'];
        $update['jetton'] = $data['jetton'];

        $res = Db::name('balance')->where('user_id',$data['user_id'])->setInc('jetton',$data['jetton']);
        if($res){
            // 添加资金流水
            $score = [];
            $score['user_id'] = $data['user_id'];
            $score['sign'] = 0;
            $score['money'] = $data['jetton'];
            $score['balance_type'] = 2;
            $score['from_type'] = 8;
            $score['create_time'] = time();
            Db::name('balance_log')->insert($score);
            return true;
        }else{
            return false;
        }
    }

    
    public function getRecharge(){

    }

    // 获取上级名称
    public function getrecommend($pid)
    {
        $res = Db::name('users')->where('user_id',$pid)->find();
        if($res){
            return $res['user_name'];
        }else{
            return "";
        }
    }
    //重置二级密码
    public function memberCheckPayPwd($id){
        $res = $this->where("id = ".$id)->update(['pay_password'=>md5("88888888")]);
        if($res){
            $this->return ['status'] = 1;
            $this->return ['msg'] = "修改成功";
        }else{
            $this->return ['status'] = 0;
            $this->return ['msg'] = "修改失败";
        }
        return $this->return;
    }

    // 后台添加会员
    public function add($data)
    {
        // 查询用户是否存在
        $users = $this->where('user_phone',$data['mobile'])->find();
        if($users)
        {
            $this->return ['status'] = 0;
            $this->return ['code'] = -1;
            $this->return ['msg'] = "用户名已存在";
            return $this->return;
        }

        // 查询推荐人是否存在
        if($data['p_phone'] != "")
        {
            $user = $this->where('user_phone',$data['p_phone'])->find();
            if($user == "")
            $this->return ['status'] = 0;
            $this->return ['code'] = -1;
            $this->return ['msg'] = "推荐人不存在";
            return $this->return;
        }

        $update['user_name'] = $data['username'];
		$update['bankId'] = $data['bankId'];
		$update['user_phone'] = $data['mobile'];
        $update['realname'] = $data['realname'];
        $update['agent'] = $data['agent'];
        $update['bankName'] = $data['bankName'];
        $update['pid'] = 0;
        $update['user_pwd'] = md5('123456');
        $update['create_time'] = time();
        $res = $this->insert($update);
        if($res)
        {
            $this->return ['status'] = 1;
            $this->return ['code'] = 1;
            $this->return ['msg'] = "添加成功";
            return $this->return;
        }

        
    }


    // 获取银行列表
    public function getBankName(){
        $res = Db::name('bank')->where('dataFlag',1)->select();
        if($res){
            return $res;
        }
    }
    //解冻积分
    public function memberThaw($id){
        $userInfo = $this->getMember($id);
        if($userInfo['free_integral'] <= 0){
            $this->return ['status'] = 0;
            $this->return ['msg'] = "您还没有可解冻的积分！";
        }else{
            $res = $this->where("id = ".$id)->update(['free_integral'=>0,'goods_integral'=>$userInfo['goods_integral'] + $userInfo['free_integral']]);
            if($res){
                $this->return ['status'] = 1;
                $this->return ['msg'] = "解冻成功";
            }else{
                $this->return ['status'] = 0;
                $this->return ['msg'] = "解冻失败";
            }
        }
        return $this->return;
    }
    //会员积分调整
    public function memberRecharge(){
        $balance_type = input("post.balance_type");
        $price = input("post.price");
        $uid = input("post.uid");
        $query = [
            'uid' => $uid,
            'price' => $price,
            'msg' => '后台充值',
            'create_time' => date('Y-m-d H:i:s'),
            'balance_type' => $balance_type,
            'form_type' => 1
        ];
        $money = $add = Db::name("member")
            ->field('sale_money,adv_money,goods_integral,free_integral')
            ->where('id',$uid)
            ->find();
        if($money['sale_money']+$price < 0){
            $data = [
                'status'=> -1,
                'msg' => "销售佣金不能为负数"
            ];
        }else if($money['adv_money']+$price < 0){
            $data = [
                'status'=> -1,
                'msg' => "广告佣金不能为负数"
            ];
        }else if($money['goods_integral']+$price < 0){
            $data = [
                'status'=> -1,
                'msg' => "商品积分不能为负数"
            ];
        }else if($money['free_integral']+$price < 0){
            $data = [
                'status'=> -1,
                'msg' => "冻结积分不能为负数"
            ];
        }else{
            $sql = Db::name("account_log")
                ->insert($query);
            if($balance_type == 1){
                $moeny = 'sale_money';
            }elseif ($balance_type == 2){
                $moeny = 'adv_money';
            }elseif ($balance_type == 3){
                $moeny = 'goods_integral';
            }else{
                $moeny = 'free_integral';
            }
            $add = Db::name("member")
                ->where('id',$uid)
                ->setInc($moeny,$price);
            if($add){
                $data = [
                    'status'=> 1,
                    'msg' => "充值成功"
                ];
            }else{
                $data = [
                    'status'=> 0,
                    'msg' => "充值失败"
                ];
            }
        }
        return $data;
    }
}
?>
